//
//  EmoticonAttachment.swift
//  14-表情键盘
//
//  Created by 高伦展 on 2018/2/23.
//  Copyright © 2018年 glz. All rights reserved.
//

import UIKit

//表情附件
class EmoticonAttachment: NSTextAttachment {

    //表情模型
    var emoticon:Emoticon
    
    init(emoticon:Emoticon){
        self.emoticon = emoticon
        
        super.init(data: nil, ofType: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //将当前附件
    func imageText(font:UIFont) -> NSAttributedString {
        
        image = UIImage(contentsOfFile:emoticon.imagePath)
        
        //‘线高’表示字体的高度
        let lineHeight = font.lineHeight
        //frame = center + bounds * transform
        //
        bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        
        //获取图片文本
        let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment:self))
        //'添加'字体 uikit.framework
        imageText.addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: 1))
        
        return imageText
    }
}
